package beans;

import java.util.Enumeration;
import java.util.Hashtable;
import store.ShopInventory;
import store.ShopInventoryDB;
import store.ShopItem;

/**
 * Keeps track of the information for this customer, including
 * name, address, and shopping cart.
 *
 * @author dale
 */
public class Customer {

    /** Customer name */
    private String name = "Change these by using";
    /** Customer address */
    private String address = "the managed bean facility";
    /** Use a hash table so that changes in quantities will replace old quantities */
    private Hashtable<Integer, ShopItem> cartItems;
    /** Total of items in cart is calculated in getCartItemBeans() */
    private float total = 0.0f;
    /** A copy of the entire inventory.  Inefficient!! */
    private ShopItem[] shopItems;
    /** The index of the first item displayed on the page */
    private int itemNumber;
    /** The number of items to be displayed on each page */
    private int itemsPerPage;

    /**
     * Constructor creates the data structure
     */
    public Customer() {
        cartItems = new Hashtable<Integer, ShopItem>();
        // This is inefficient, but will suffice for this lab
        shopItems = ShopInventoryDB.getArrayOfItems();
    }

    /**
     * Add the item to the cart if the quantity is >0,
     * otherwise, remove it if is was in the cart.
     *
     * @param inventoryCode The stock code
     * @param quantity The quantity purchased
     */
    public String addBeansToCart() {
        System.out.println("Customer.addBeansToCart. itemNumber=" + itemNumber);
        int itemCount = getShopItems().length;
        if (itemCount == 0) {
            return null;
        }

        // If cart is changed, reset total
        total = 0.0f;
        int i = itemNumber;
        for (     ; i < itemNumber+itemsPerPage && i < shopItems.length; i++) {
            ShopItem item = shopItems[i];
            if (item.getQuantity() > 0.0f) {
                cartItems.put(item.getInventoryNumber(), item);
            } else {
                // Since we allow using the back button,
                // get rid of anything we had that was changed to zero
                cartItems.remove(item.getInventoryNumber());
            }
        }

        // Our return value is used by the Navigation Rules
        if (i >= ShopInventory.getNumberOfItems()) {
            return "checkOut";
        }
        return "continue";
    }

    /**
     * Return the shopping cart hashtable so that the
     * purchase can be processed
     *
     * @return The shopping cart hashtable
     */
    public Hashtable<Integer, ShopItem> getCartItems() {
        return cartItems;
    }

    /**
     * Creates an array of shopping cart items.
     * Since this has quantities, it needs to be created for each user
     *
     * @return An array of the shopping cart items
     */
    public ShopItem[] getCartItemBeans() {
        ShopItem[] cartItemBeans = new ShopItem[cartItems.size()];
        Enumeration<Integer> stockNumbers = cartItems.keys();
        int i = 0;
        total = 0.0f;
        while (stockNumbers.hasMoreElements()) {
            Integer stockNumber = stockNumbers.nextElement();
            ShopItem item = cartItems.get(stockNumber);
            cartItemBeans[i++] = new ShopItem(item.getInventoryNumber(), item.getName(),
                    item.getPrice(), item.getUnit(), item.getQuantity());
            total += item.getPrice() * item.getQuantity();
        }

        return cartItemBeans;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the total
     */
    public float getTotal() {
        return total;
    }

    /**
     * @return the itemNumber
     */
    public int getItemNumber() {
        return itemNumber;
    }

    /**
     * @param itemNumber the itemNumber to set
     */
    public void setItemNumber(int itemNumber) {
        this.itemNumber = itemNumber;
    }

    /**
     * @return the itemsPerPage
     */
    public int getItemsPerPage() {
        return itemsPerPage;
    }

    /**
     * The number of items displayed on each JSF page
     * 
     * @param itemsPerPage the itemsPerPage to set
     */
    public void setItemsPerPage(int itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    /**
     * @return the shopItems
     */
    public ShopItem[] getShopItems() {

        return shopItems;
    }
}
